#ifndef _DevicesSPI_H_
#define _DevicesSPI_H_

#include "stdint.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"


#define SPI0_NSS_SET_HIGH()     gpio_set_level(GPIO_NUM_7, 1)
#define SPI0_NSS_SET_LOW()      gpio_set_level(GPIO_NUM_7, 0)

#define SPI1_NSS_SET_HIGH()     gpio_set_level(GPIO_NUM_7, 1)
#define SPI1_NSS_SET_LOW()      gpio_set_level(GPIO_NUM_7, 0)

#define SPI2_NSS_SET_HIGH()     gpio_set_level(GPIO_NUM_7, 1)
#define SPI2_NSS_SET_LOW()      gpio_set_level(GPIO_NUM_7, 0)

#define SPI2_SPEED_HIGH()
#define SPI2_SPEED_LOW()


extern spi_device_handle_t  g_spi0_handle;
extern spi_device_handle_t  g_spi1_handle;
extern spi_device_handle_t  g_spi2_handle;


void vSPI0Init(void);
void vSPI1Init(void);
void vSPI2Init(void);

#define ucSPI0WriteReadByte(ucByte)                     ucSPIWriteReadByte(g_spi0_handle, (ucByte))
#define cSPI0WriteDatas(pBuffer, iLength)               cSPIWriteDatas(g_spi0_handle, (pBuffer), (iLength))
#define cSPI0ReadDatas(pBuffer, iLength)                cSPIReadDatas(g_spi0_handle, (pBuffer), (iLength))
#define cSPI0DmaWriteDatas(pBuffer, iLength, iWaitUs)   cSPIDmaWriteDatas(g_spi0_handle, (pBuffer), (iLength), iWaitUs)

#define ucSPI1WriteReadByte(ucByte)                     ucSPIWriteReadByte(g_spi1_handle, (ucByte))
#define cSPI1WriteDatas(pBuffer, iLength)               cSPIWriteDatas(g_spi1_handle, (pBuffer), (iLength))
#define cSPI1ReadDatas(pBuffer, iLength)                cSPIReadDatas(g_spi1_handle, (pBuffer), (iLength))
#define cSPI1DmaWriteDatas(pBuffer, iLength, iWaitUs)   cSPIDmaWriteDatas(g_spi1_handle, (pBuffer), (iLength), iWaitUs)

#define ucSPI2WriteReadByte(ucByte)                     ucSPIWriteReadByte(g_spi2_handle, (ucByte))
#define cSPI2WriteDatas(pBuffer, iLength)               cSPIWriteDatas(g_spi2_handle, (pBuffer), (iLength))
#define cSPI2ReadDatas(pBuffer, iLength)                cSPIReadDatas(g_spi2_handle, (pBuffer), (iLength))
#define cSPI2DmaWriteDatas(pBuffer, iLength, iWaitUs)   cSPIDmaWriteDatas(g_spi2_handle, (pBuffer), (iLength), iWaitUs)

uint8_t ucSPIWriteReadByte(spi_device_handle_t handle, uint8_t ucByte);
int8_t cSPIWriteDatas(spi_device_handle_t handle, void *pvBuff, int32_t iLength);
int8_t cSPIReadDatas(spi_device_handle_t handle, void *pvBuff, int32_t iLength);
int8_t cSPIDmaWriteDatas(spi_device_handle_t handle, void *pvDatas, int32_t iLength, int32_t iWaitUs);


#endif
